package fr.univ.lille1.server.domain;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import org.codehaus.jackson.annotate.JsonIgnore;

@Entity
@Table(name="MESSAGE")
public class Message implements Serializable {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 8840499241422500275L;

	@Id 
	@GeneratedValue 
	private int id;
	
	@Column(nullable=false) 
	private String title;
	
	@Column(nullable=false) 
	private String content;
	
	@Column(nullable=false)  
	private float latitude;
	
	@Column(nullable=false)  
	private float longitude;
	
	@Temporal(TemporalType.TIMESTAMP)
	@Column(nullable=false)  
	private Date addDate;
	
	@OneToOne
	@JoinColumn(nullable=false)
	private User user;

	@OneToMany
	@JoinTable(name="MESSAGES_LIKES")
	private List<User> likes;
	
	@OneToMany
	@JoinTable(name="MESSAGES_DISLIKES")
	private List<User> dislikes;
	
	@OneToMany
	@JoinTable(name="MESSAGES_ALERTS")
	private List<User> alerts;
	
	public Message() {
		super();
	}

	public Message(String title, String content, float latitude,
			float longitude, Date addDate, User user) {
		super();
		this.title = title;
		this.content = content;
		this.latitude = latitude;
		this.longitude = longitude;
		this.addDate = (Date) addDate.clone();
		this.user = user;
	}

	public int getId() {
		return id;
	}
	
	public String getTitle() {
		return title;
	}
	
	public void setTitle(String title) {
		this.title = title;
	}
	
	public String getContent() {
		return content;
	}
	
	public void setContent(String content) {
		this.content = content;
	}
	
	public float getLatitude() {
		return latitude;
	}
	
	public float getLongitude() {
		return longitude;
	}
	
	public Date getAddDate() {
		return (Date) addDate.clone();
	}
	
	public User getUser() {
		return user;
	}
	
	public void like(User user){
		if(!this.likes.contains(user)){
			this.likes.add(user);
			this.dislikes.remove(user);
		}
	}
	
	public void dislike(User user){
		if(!this.dislikes.contains(user)){
			this.dislikes.add(user);
			this.likes.remove(user);
		}
	}
	
	public void alert(User user){
		if(!this.alerts.contains(user)){
			this.alerts.add(user);
		}
	}
	
	@JsonIgnore
	public List<User> getLikes() {
		return likes;
	}

	@JsonIgnore
	public List<User> getDislikes() {
		return dislikes;
	}

	@JsonIgnore
	public List<User> getAlerts() {
		return alerts;
	}

	public int getLikesCount(){
		return likes.size();
	}
	
	public int getDislikesCount(){
		return dislikes.size();
	}
	
	public int getAlertCount(){
		return alerts.size();
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + id;
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj){
			return true;
		}
		if (obj == null){
			return false;
		}
		if (getClass() != obj.getClass()){
			return false;
		}
		Message other = (Message) obj;
		if (id != other.id){
			return false;
		}
		return true;
	}
	
	
}
